var userId =function (){
    if(sessionStorage.getItem('loginUser')===null){
        return null;
    }  else{
        console.log(1111111)
        return JSON.parse(sessionStorage.getItem('loginUser')).userID;
    }
};
function getUrlParam(name) {
    //构造一个含有目标参数的正则表达式对象
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    //匹配目标参数
    var r = window.location.search.substr(1).match(reg);
    //返回参数值
    if(r != null) {
        return decodeURI(r[2]);
    }
    return null;
}
window.onload = function (){

    if (null === userId()) {
        alert("请先登录！");
        window.location.href = backend + "login&register.html";
    }else{
        document.getElementById("page_login").innerHTML="欢迎 "
            + "<a class=\"pageullireg\">"+JSON.parse(sessionStorage.getItem('loginUser')).userInfo
            + "</a>&nbsp;";

    }
    console.log(getUrlParam("bookId"))
    let bookId = getUrlParam("bookId");
    let bookName = getUrlParam("bookName");
    let url = backend+"book/getBookByID?bookId="+bookId;
    if(null==bookId&&null==bookName){
        alert("请求异常:无参数！")
    }else{
        if(null!=bookName){
            if(null==bookId){
                url = backend+"book/getBookByName?bookName="+bookName;
            }

        }
    }
    $.ajax({
        url:url,
        success:function (dataJ){
            console.log(dataJ)
            let bookInfo = JSON.parse(dataJ).bookInfo;
            if(bookId==null&&bookName!=null){
                location.href = location.href+"&bookId="+bookInfo.bookId;
            }
            $('#bookName').text(bookInfo.bookName);
            $('#bookDescription').text(bookInfo.bookDescription);
            $('#bookPrice').text('¥'+bookInfo.bookPrice);
            $('#originalBookPrice').text('定价¥'+(bookInfo.bookPrice*1.6).toFixed(1));
            $('#eBookPrice').text('¥'+(bookInfo.bookPrice*0.6).toFixed(1));
            $('#bookPic').attr("src",backend+bookInfo.bookPicture)
        }
    })
}